package com.emcom.gotour.validation;

import java.math.BigDecimal;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.emcom.gotour.po.TourContent;

public class TourValidator implements Validator {

	public boolean supports(Class clazz) {
		return TourContent.class.equals(clazz);
	}

	public void validate(Object object, Errors errors) {
		TourContent tourContent = (TourContent) object;
		ValidationUtils.rejectIfEmpty(errors, "typeId", TourValidatorMessage.TYPE_ID.value());
		ValidationUtils.rejectIfEmpty(errors, "category", TourValidatorMessage.CATEGORY.value());
		ValidationUtils.rejectIfEmpty(errors, "subject", TourValidatorMessage.SUBJECT.value());
		ValidationUtils.rejectIfEmpty(errors, "location", TourValidatorMessage.LOCATION.value());
		int days = tourContent.getDays();
		if(days <= 0) {
			errors.rejectValue("age", TourValidatorMessage.DAYS.value());
		}
		BigDecimal cost = tourContent.getCost();
		if(cost.compareTo(BigDecimal.ZERO) < 1) {
			errors.rejectValue("cost", TourValidatorMessage.COST.value());
		}
	}

}
